<!DOCTYPE html>
<!--
登录、权限、分页、搜索
-->
<?php
require 'lyb_conn.php';
session_start();
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title>显示页面</title>
        <style>
            .ly_1{color: black};
        </style>
        <style>
            .ly_2{color: blue};
        </style>
        <style>
            .ly_3{color: red};
        </style>
        <script type="text/javascript" src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
        <!--
        js的click事件
        <script type="text/javascript">
            $(function () {
                $("#zd").click(function () {
                    alert('123');
                });
            }); 
        </script>
         $(".con").html(data);
        -->

        <!--
        写在js里的ajax事件，可以传到其他php页面并获取，但是在本页面无法获取，待测试
        $.post('test.php',{p:page_appoint},function(data){
        写入相关操作
        })  
        -->
        <script type="text/javascript">
            function zd() {
                var page_appoint = document.getElementById("p1").value;
                window.location.href = '?p=' + page_appoint;
            }

            function cx() {
                var cx_content = document.getElementById("p2").value;
                //$.post('lyb_cx.php', {name: cx_content});
                window.location.href = 'lyb_search.php?kw=' + cx_content;
            }
            function cz() {
                window.location.href = 'index.php';
            }
        </script>
    </head>
    <body>
        <?php
        $kw = $_GET[kw];
        ?>
        <div align="center">
            请输入用户名：
            <input type="text" width="20%" id="p2">
            <input type="button" id="cx" name="cx" value="查询" onclick="cx()">
            <input type="button" id="cz" name="cz" value="重置" onclick="cz()">           

            <?php
            if ($_SESSION['user']) {
                echo "<a href='lyb_logout.php' style='float:right'>注销</a>";
                echo "<span style='float:right'>欢迎用户：<span class='ly_3'>" . $_SESSION['user'] . "</span></span>";
            } else {
                echo "<a href='lyb_register.php' style='float:right'>注册</a>";
                echo "<a href='lyb_login.php' style='float:right'>登录</a>";
            }
            //<a href="lyb_login.php" style="float:right">登录</a>
            //<a href="lyb_register.php" style="float:right">注册</a>
            ?>
        </div>
        <br>
        <div align="center">
            以下是用户名为：<span class="ly_3"><?php echo $kw ?></span> 的搜索结果
        </div>
        <br>
        <table border="1" align="center" cellspacing="0" width="70%">
            <tr>
                <td align="center">序号</td>
                <td align="center">用户名</td>
                <td align="center">标题</td>
                <td align="center">内容</td>
                <td align="center">时间</td>
                <td align="center" colspan="2">操作</td>
            </tr>
            <?php
            $page_size = 20; //每页显示数量
            $page_now = $_GET[p]; //获取当前页，p为用户指定的当前页数
            //$kw=$_GET[kw];
            if (!isset($page_now)) {
                $page_now = 1;
            }
            /*
              if(!isset($kw)){
              echo "<script type=text/javascript>alert('请输入查询条件！')</script>";
              }
             * 
             */
            $page_start = ($page_now - 1) * $page_size; //设置查询起始值
            $zscx = "select count(*) as total from n1 where user like'%$kw%'"; //查询表单总数据量
            $sjcx = "select * from n1 where user like'%$kw%' order by add_date desc limit $page_start,$page_size"; //分页查询
            $result = mysqli_query($con, $sjcx);
            $zongshu = mysqli_query($con, $zscx);
            $rows_count = mysqli_fetch_array($zongshu);
            $rows_count = $rows_count['total']; //总数量
            $page_count = ceil($rows_count / $page_size); //总页数,ceil函数返回不小于处理结果的下一个整数
            $i = 1;
            $i = ($page_now - $i) * $page_size;
            while ($rows = mysqli_fetch_array($result)) {
                $i += 1;
                ?>
                <tr> 
                    <td class="ly_1" align="center"><?php echo $i ?></td>
                    <td class="ly_1"><?php echo $rows[user] ?></td>
                    <td class="ly_1"><?php echo $rows[title] ?></td>
                    <td class="ly_1"><?php echo $rows[content] ?></td>
                    <td class="ly_1"><?php echo $rows[add_date] ?></td>
                    <?php
                    $_SESSION['url']=$_SERVER["REQUEST_URI"];
                    if ($_SESSION['user'] == 'admin') {
                        echo "<td class='ly_2'><a href='lyb_edit.php?id=$rows[id]'>编辑</a></td>";
                        echo "<td class='ly_2'><a href='lyb_delete.php?id=$rows[id]'>删除</a></td>";
                    } elseif ($_SESSION['user'] == "") {
                        echo "<td class='ly_3'>登录之后更多权限</td>";
                    } else {
                        if ($rows[user] == $_SESSION['user']) {
                            echo "<td class='ly_2'><a href='lyb_edit.php?id=$rows[id]'>编辑</a></td>";
                            echo "<td class='ly_2'><a href='lyb_delete.php?id=$rows[id]'>删除</a></td>";
                        } else {
                            echo "<td class='ly_3' colspan='2'>无权限</td>";
                        }
                    }
                    /*
                      <td class="ly_2"><a href="<?php echo 'lyb_edit.php?id=' . $rows[id] ?>">编辑</a></td>
                      <td class="ly_2"><a href="<?php echo 'lyb_delete.php?id=' . $rows[id] ?>">删除</a></td>
                     *
                     */
                    ?>
                </tr>
                <?php
            }
            ?>
        </table>
        <br>
        <div align="center">
            <?php
            $url = $_SERVER["PHP_SELF"] . "?kw=" . $kw . "&p="; //拼接url
            //echo $_SERVER["REQUEST_URI"];
            echo '<br>';
            /* 首页和上一页 */
            if ($page_now > 1) {
                echo "<a href=" . $url . "1" . ">首页</a>";
                echo '&nbsp;';
                echo "<a href=" . $url . ($page_now - 1) . ">上一页</a>";
                echo '&nbsp;';
            } else {
                echo "<a disable='true'>首页</a>";
                echo '&nbsp;';
                echo "<a disable='true'>上一页</a>";
                echo '&nbsp;';
            }

            /* 判断1 2 3 4 5页数的排序 */
            if ($page_count <= 10) {
                for ($j = 1; $j <= $page_count; $j++) {
                    echo "<a href=" . $url . $j . ">" . $j . "</a>";
                    echo '&nbsp';
                }
            } elseif ($page_now < 6) {
                for ($j = 1; $j <= 10; $j++) {
                    echo "<a href=" . $url . $j . ">" . $j . "</a>";
                    echo '&nbsp;';
                }
            } elseif ($page_now >= ($page_count - 5)) {
                for ($j = 9; $j >= 0; $j--) {
                    echo "<a href=" . $url . ($page_count - $j) . ">" . ($page_count - $j) . "</a>";
                    echo '&nbsp';
                }
            } else {
                $j = $page_now;
                for ($j; $j <= $j + 5; $j++) {
                    echo "<a href=" . $url . ($j - 4) . ">" . ($j - 4) . "</a>";
                    echo '&nbsp;';
                    $k += 1;
                    if ($k >= 10) {
                        break;
                    }
                }
            }

            /* 下一页和末页 */
            if ($page_now < $page_count) {
                echo "<a href=" . $url . ($page_now + 1) . ">下一页</a>";
                echo '&nbsp;';
                echo "<a href=" . $url . $page_count . ">末页</a>";
            } else {
                echo "<a disable='true'>下一页</a>";
                echo '&nbsp;';
                echo "<a disable='true'>末页</a>";
            }
            echo '&nbsp;';
            echo "共:<span class='ly_3'>" . $rows_count . "</span>条记录,";
            echo '&nbsp;';
            echo "<span class='ly_3'>" . $page_now . "/" . $page_count . "</span>页";

            /* 跳页 */
            /*
              echo '&nbsp';
              echo "<input style='width:40px; type='text' name='p'>";
              echo '&nbsp';
              echo "<input type='submit' name='zd' value='转到'>";
             */
            ?>
            <input style="width:40px;" type="text" id="p1" name="p1">
            <input type="button" id="zd" value="转到" name="zd" onclick="zd()">
        </div>
        <br>
        <div align="center">
            <a href="lyb_add.php" class="ly_1">继续添加</a>
        </div>
    </body>
</html>

